﻿Imports MyDLL

Public Class Form11

    Dim Nav As New MyDLL.RecordNav

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim C As String = TextBox7.Text
        Dim Month As String = ComboBox1.SelectedItem
        Dim Year As String = TextBox1.Text

        If CheckBox1.Checked Then
            Dim Datxml As XElement = _
            <Date>
                <Month><%= Month %></Month>
                <Year><%= Year %></Year>
            </Date>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Appointments>" & vbCr _
                                      & "<Type>Appointments</Type>" & vbCr & Datxml.ToString & vbCr
        End If

        If CheckBox2.Checked Then
            Dim Datxaml As XElement = _
            <Paragraph FontFamily="Arial" FontSize="20pt">
                Appointments for <%= Month %> , <%= Year %>
            </Paragraph>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & _
            "<FlowDocument xmlns=" & C & "http://schemas.microsoft.com/winfx/2006/xaml/presentation" & C & ">" & vbCr & Datxaml.ToString & vbCr
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Count As String = TextBox6.Text

        If CheckBox1.Checked Then
            Dim Counter As XElement = _
            <Counter>
                <Count><%= Count %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter.ToString & vbCr & "</Appointments>"
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & "</FlowDocument>"
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WriteData()
    End Sub

    Private Sub WriteData()
        Dim ID As Integer = TextBox6.Text
        Dim Dat As String = TextBox15.Text

        Dim Tim1 As String = TextBox2.Text
        Dim App1 As String = TextBox3.Text
        Dim Tim2 As String = TextBox5.Text
        Dim App2 As String = TextBox4.Text
        Dim Tim3 As String = TextBox10.Text
        Dim App3 As String = TextBox9.Text
        Dim Tim4 As String = TextBox12.Text
        Dim App4 As String = TextBox11.Text
        Dim Tim5 As String = TextBox14.Text
        Dim App5 As String = TextBox13.Text

        If CheckBox3.Checked Then
            ID = ID + 1
            TextBox6.Text = ID
        End If

        If CheckBox1.Checked Then
            Dim Appxml As XElement = _
            <Day>
                <Date id=<%= ID %>><%= Dat %></Date>
                <Entries>
                    <Tim1 id=<%= ID %>><%= Tim1 %></Tim1>
                    <App1 id=<%= ID %>><%= App1 %></App1>
                    <Tim2 id=<%= ID %>><%= Tim2 %></Tim2>
                    <App2 id=<%= ID %>><%= App2 %></App2>
                    <Tim3 id=<%= ID %>><%= Tim3 %></Tim3>
                    <App3 id=<%= ID %>><%= App3 %></App3>
                    <Tim4 id=<%= ID %>><%= Tim4 %></Tim4>
                    <App4 id=<%= ID %>><%= App4 %></App4>
                    <Tim5 id=<%= ID %>><%= Tim5 %></Tim5>
                    <App5 id=<%= ID %>><%= App5 %></App5>
                </Entries>
            </Day>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Appxml.ToString & vbCr

        End If

        If CheckBox2.Checked Then

            Dim Appxaml As XElement = _
            <List FontFamily="Arial" FontSize="11pt">
                <ListItem><Paragraph>ID:<%= ID %></Paragraph></ListItem>
                <ListItem><Paragraph>Date:<%= Dat %></Paragraph></ListItem>
                <ListItem><Paragraph><%= Tim1 %> : <%= App1 %></Paragraph></ListItem>
                <ListItem><Paragraph><%= Tim2 %> : <%= App2 %></Paragraph></ListItem>
                <ListItem><Paragraph><%= Tim3 %> : <%= App3 %></Paragraph></ListItem>
                <ListItem><Paragraph><%= Tim4 %> : <%= App4 %></Paragraph></ListItem>
                <ListItem><Paragraph><%= Tim5 %> : <%= App5 %></Paragraph></ListItem>
            </List>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & Appxaml.ToString & vbCr
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        LoadData()
    End Sub

    Private Sub LoadData()
        Try
            Me.Text = "Appointments Database" & " - " & Form1.TextBox3.Text

            Dim ID As String = TextBox6.Text

            Dim Appointments As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Dat As XElement = _
                <Root>
                    <%= From el In Appointments.<Day>.<Date> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox15.Text = Dat

            Dim Tim1 As XElement = _
                <Root>
                    <%= From el In Appointments.<Day>.<Entries>.<Tim1> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox2.Text = Tim1

            Dim App1 As XElement = _
        <Root>
            <%= From el In Appointments.<Day>.<Entries>.<App1> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox3.Text = App1

            Dim Tim2 As XElement = _
                <Root>
                    <%= From el In Appointments.<Day>.<Entries>.<Tim2> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox5.Text = Tim2

            Dim App2 As XElement = _
        <Root>
            <%= From el In Appointments.<Day>.<Entries>.<App2> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox4.Text = App2

            Dim Tim3 As XElement = _
                <Root>
                    <%= From el In Appointments.<Day>.<Entries>.<Tim3> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox10.Text = Tim3

            Dim App3 As XElement = _
        <Root>
            <%= From el In Appointments.<Day>.<Entries>.<App3> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox9.Text = App3

            Dim Tim4 As XElement = _
                <Root>
                    <%= From el In Appointments.<Day>.<Entries>.<Tim4> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox12.Text = Tim4

            Dim App4 As XElement = _
        <Root>
            <%= From el In Appointments.<Day>.<Entries>.<App4> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox11.Text = App4

            Dim Tim5 As XElement = _
                <Root>
                    <%= From el In Appointments.<Day>.<Entries>.<Tim5> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox14.Text = Tim5

            Dim App5 As XElement = _
        <Root>
            <%= From el In Appointments.<Day>.<Entries>.<App5> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox13.Text = App5

            Dim Month As XElement = _
               <Root>
                   <%= From el In Appointments.<Date>.<Month> _
                       Select el %>
               </Root>
            Dim Monthet As String = Month
            ComboBox1.SelectedItem = Monthet

            Dim Year As XElement = _
               <Root>
                   <%= From el In Appointments.<Date>.<Year> _
                       Select el %>
               </Root>
            TextBox1.Text = Year


            Dim Count As XElement = _
        <Root>
            <%= From el In Appointments.<Counter>.<Count> _
                Select el %>
        </Root>

            Dim Counter As String = Count

            Label7.Text = "There are " & Counter & " record(s) in the XML file"
        Catch ex As Exception
            MsgBox("No file has been loaded.")

        End Try

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            Do Until TextBox6.Text = TextBox8.Text
                CheckBox3.CheckState = CheckState.Unchecked
                Dim ID As Integer = TextBox6.Text
                ID = ID + 1
                TextBox6.Text = ID
                LoadData()
                WriteData()
            Loop
        Catch ex As Exception
            MsgBox("Choose a proper range.")
        End Try
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Nav.GoToFirst(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Nav.PrevRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Nav.NextRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Nav.GoToLast(TextBox6, Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

        LoadData()
    End Sub
End Class